package com.secure.sample.api;

import lombok.Data;

/**
 * 基础响应
 *
 * @author duchao
 */
@Data
public class BaseResponse<T> {

    private Integer code;
    private String msg;
    private T data;

    public BaseResponse() {
    }

    public BaseResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public BaseResponse(IResponseEnum responseEnum, T data) {
        this.code = responseEnum.getCode();
        this.msg = responseEnum.getMsg();
        this.data = data;
    }

    /**
     * 成功
     *
     * @param data 数据
     * @param <T>  数据类型
     * @return
     */
    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<>(BaseResponseEnum.SUCCESS, data);
    }

    /**
     * 失败
     *
     * @return
     */
    public static BaseResponse failure() {
        return new BaseResponse<>(BaseResponseEnum.FAILURE, null);
    }

    /**
     * 失败
     *
     * @param msg 消息提示
     * @return
     */
    public static BaseResponse failure(String msg) {
        return new BaseResponse<>(BaseResponseEnum.FAILURE.getCode(), msg, null);
    }

    /**
     * 失败
     *
     * @param responseEnum 响应枚举
     * @return
     */
    public static BaseResponse failure(IResponseEnum responseEnum) {
        return new BaseResponse<>(responseEnum, null);
    }
}
